package com.graphqljava.demo.gql.service.impl;

import com.graphqljava.demo.gql.entity.AchieveInfo;
import com.graphqljava.demo.gql.entity.PersonInfo;
import com.graphqljava.demo.gql.service.PersonInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * com.graphqljava.demo.gql.service
 *
 * @author Oscar.Chen
 * @date 6/16/2020 12:28 AM
 */
@Service
public class PersonInfoServiceImpl implements PersonInfoService {

    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public Optional<PersonInfo> findPersonInfoById(String id) {
        Query query = new Query(Criteria.where("id").is(id));
        return Optional.ofNullable(mongoTemplate.findOne(query, PersonInfo.class));
    }

    @Override
    public List<PersonInfo> allPersonInfos() {
        return mongoTemplate.findAll(PersonInfo.class);
    }

    @Override
    public List<PersonInfo> allPersonInfosByPersonId(String personId) {
        Query query = new Query(Criteria.where("personId").is(personId));
        return mongoTemplate.find(query, PersonInfo.class);
    }

    @Override
    public List<PersonInfo> allPersonInfosByProjectId(String projectId) {
        Query query = new Query(Criteria.where("project_id").is(projectId));
        return mongoTemplate.find(query, PersonInfo.class);
    }
}
